package br.edu.ufcg.embedded.eolica.MIDlet;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;

import br.edu.ufcg.embedded.eolica.domain.User;
import br.edu.ufcg.embedded.eolica.exception.FacadeException;
import br.edu.ufcg.embedded.eolica.facade.Facade;
import br.edu.ufcg.embedded.eolica.util.Properties;
import br.edu.ufcg.embedded.eolica.view.AboutForm;
import br.edu.ufcg.embedded.eolica.view.ConsultCanvas;
import br.edu.ufcg.embedded.eolica.view.ConsultForm;
import br.edu.ufcg.embedded.eolica.view.ErrorAlert;
import br.edu.ufcg.embedded.eolica.view.HelpForm;
import br.edu.ufcg.embedded.eolica.view.PersonalInfoForm;
import br.edu.ufcg.embedded.eolica.view.SplashScreen;

/**
 * MIDlet to receive graphics from a Server, given a specific station and time
 * interval.
 * 
 * @author Danilo Leandro
 * @author thiagobrunoms
 */
public class UsinaEolicaMIDlet extends MIDlet implements CommandListener {
	
	private Display mDisplay;
	private List menuList;
	//private GraphListType graphListType;
	private ConsultCanvas consultCanvas;
	private PersonalInfoForm settingsForm;
	private HelpForm helpForm;
	private AboutForm aboutForm;
	private Command comExit;
	private User user;
	private Facade facade;
	private String[] menuStrings;
	private SplashScreen splash;
	
	private static Properties  prop;
	
	public UsinaEolicaMIDlet() {
		mDisplay = Display.getDisplay(this);
		facade = Facade.getInstance();
		initComponents();
	}
	
	private void initComponents() {
		String lang = getAppProperty("Eolica-Lang");
		prop = Properties.getInstance();
		prop.setDefLang(lang);
		prop.load(".config.txt");
		
		menuStrings = new String[]{ prop.getProperty("label.consult"), prop.getProperty("label.settings"), 
				prop.getProperty("label.help"), prop.getProperty("label.about"), prop.getProperty("label.exit") };
		
		menuList = new List("Eletrobras - Menu", List.IMPLICIT,
				menuStrings, null);
		comExit = new Command(prop.getProperty("label.exit"), Command.EXIT, 0);
		menuList.addCommand(comExit);
		menuList.setCommandListener(this);
		
		if (helpForm == null)
			helpForm = new HelpForm(mDisplay, menuList, prop.getProperty("label.help"));

		if (aboutForm == null)
			aboutForm = new AboutForm(mDisplay, menuList, prop.getProperty("label.about"));
	}

	public void startApp() {
		splash = new SplashScreen(null, mDisplay, menuList);
        splash.setImage("/nova-eletrobras2.gif");
        splash.setTimeOut(5000);
        splash.start();

		//mDisplay.setCurrent(menuList);
	}
	
    public static Properties getProp() {
    	return prop;
    }

	public void commandAction(Command cmd, Displayable s) {
		if (cmd == List.SELECT_COMMAND) {
			int index = menuList.getSelectedIndex();
			
			switch (index) {
			case 0:
				try {
					user = facade.getUser();
					if (user != null) {
						if(consultCanvas == null)
							consultCanvas = new ConsultCanvas(mDisplay, menuList, prop.getProperty("label.consult"));
							
						mDisplay.setCurrent(consultCanvas);
					} else {
						settingsForm = new PersonalInfoForm(mDisplay, prop.getProperty("label.userinfo"), menuList);
						mDisplay.setCurrent(new ErrorAlert(prop.getProperty("label.error"), 
								prop.getProperty("error.user")), settingsForm);
					}
				} catch (FacadeException e) {
					mDisplay.setCurrent(new ErrorAlert(prop.getProperty("label.error"), 
							prop.getProperty("error.internal")), menuList);
				}
				
				break;
			case 1:
				if (settingsForm == null)
					settingsForm = new PersonalInfoForm(mDisplay, prop.getProperty("label.userinfo"), menuList);
				
				mDisplay.setCurrent(settingsForm);
				break;
			case 2:
				mDisplay.setCurrent(helpForm);
				break;
			case 3:
				mDisplay.setCurrent(aboutForm);
				break;
			case 4:
				notifyDestroyed();
				break;
			default:
				break;
			}
		} else if (cmd == comExit)
			notifyDestroyed();

	}

	public void pauseApp() {}
	public void destroyApp(boolean unconditional) {}
}
